package control;

import java.util.ArrayList;
import java.util.Scanner;

public class Menu {
	
	private static String[] yesOrNo = {"Yes", "No"};
	
	/**
	 *  getMenuOption - gets a user to select a valid choice from a menu
	 * Preconditions: none
	 * Postconditions: a choice (as an index into the array sent in) is
	 *                 returned
	 * @param prompt: The prompt that goes with the menu
	 * @param options: the options the user has to choose from
	 * @return choice: the user's selection
	 */
	public static int getMenuOption(String prompt, String[] options){
		int choice = -1;
		Scanner in = new Scanner(System.in);
		while (choice < 0 || choice > options.length){
			System.out.println(prompt);
			for (int i = 0; i < options.length; i++)
			{
				System.out.printf("%d\t%s\n", i + 1, options[i]);
			} 
			try{
				choice = in.nextInt();
			} catch (Exception e){
				System.out.println("Please enter an integer, friend.");
			}
			if (choice < 1 || choice > options.length){
				System.out.println("Please enter one of the menu choices.");
			}
		}
		return choice - 1;
	}
	
	/**
	 *  getMenuOption - gets a user to select a valid choice from a menu
	 * Preconditions: none
	 * Postconditions: a choice (as an index into the array sent in) is
	 *                 returned
	 * @param prompt: The prompt that goes with the menu
	 * @param options: the options the user has to choose from
	 * @return choice: the user's selection
	 */
	public static int getMenuOption(String prompt, ArrayList<String> options){
		String[] newOptions = new String[options.size()];
		int index = 0;
		for (String s: options){
			newOptions[index] = s;
			index++;
		}
		return getMenuOption(prompt, newOptions);
	}
	
	public static boolean yesOrNoMenu(String prompt){
		return getMenuOption(prompt, yesOrNo) == 0;
	}

}
